package com.itheima.info.manager.dao;

import com.itheima.info.manager.domain.Student;

public class StudentDao implements BaseStudentDao{
    //创建静态学生数组用于保存学生信息
    private static Student[] student = new Student[5];
    static {
        student[0] = new Student("001","张三","20","2000-11-11");
        student[1] = new Student("002","张小三","21","1999-11-11");
        student[2] = new Student("003","张老三","19","2001-11-11");
    }
    public boolean addStudent(Student stu) {
        //遍历数组判断数组是否满了,如果没有满把stu添加到student数组对应的index索引中.
        int index = -1;
        //遍历student数组,如果数组中元素为null,则将对应的索引i赋值给index.
        for (int i = 0; i < student.length; i++) {
            if (student[i] == null){
                index = i;
                break;
            }
        }
        //若果index等于-1,则说明数组已经存满了;否则将传递来的stu对象储存到student对应的index索引中.
        if (index == -1){
            return false;
        }else {
            student[index] = stu;
            return true;
        }
    }

    //创建findAllStudent()方法,将Student[]数组返回.
    public Student[] findAllStudent() {
        return student;
    }

    public boolean deleteStudent(String id) {
        int index = getIndex(id);
        student[index] = null;
        return true;
    }

    public boolean updateStudent(Student stu) {
        int index = getIndex(stu.getId());
        student[index] = stu;
        return true;
    }
    public int getIndex(String id){
        int index = -1;
        for (int i = 0; i < student.length; i++) {
            if (student[i] != null && student[i].getId().equals(id)){
                index = i;
            }
        }
        return index;
    }
}
